//
//  LZMyTicketViewController.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/13.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZMyTicketViewController.h"
#import "LZMyTicketCell.h"
#import "LZTicketModel.h"

#import "SPPlaceholderCell.h"
static NSString *const placeCellID = @"placeholders";
static int _page;

@interface LZMyTicketViewController ()<UITableViewDelegate,UITableViewDataSource> {
    LZUserInfo *userInfo;
    // 优惠券模型数组
    NSMutableArray <LZTicketModel *>*_ticketArray;
}
/** tableView */
@property (nonatomic , strong) UITableView *mainTableView;
@property (nonatomic,assign) BOOL  isHave;
@end

static NSString *cellID = @"lz_myTicketCell";
@implementation LZMyTicketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    userInfo = [LZUserInfo getUserInfo];
    _ticketArray = [NSMutableArray array];
    self.isHave = YES;
    self.navigationItem.title = @"优惠卷";
    [self createTableView];
    [_mainTableView.mj_header beginRefreshing];
}

// 创建tableView
- (void)createTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] init];
        [self.view addSubview:_mainTableView];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;

        _mainTableView.backgroundColor = KHColorFromRGB(0xf8f8f8);
        _mainTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_mainTableView registerClass:[LZMyTicketCell class] forCellReuseIdentifier:cellID];
         [_mainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPPlaceholderCell class]) bundle:nil] forCellReuseIdentifier:placeCellID];
        [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    
    // 下拉刷新
    ESWeak(self, weakSelf);
    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf pullDownData];
        [weakSelf.mainTableView.mj_header endRefreshing];
        
    }];
    
    // 上啦加载
    _mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf pullUpData];
        [weakSelf.mainTableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.isHave ==YES ? _ticketArray.count : 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10*ysIZE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHave == YES) {
        return 100*ysIZE;
    }else {
        return SCREEN_Height - 104;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHave == YES) {
        LZMyTicketCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.ticketModel = _ticketArray[indexPath.section];
        return cell;
    }else {
        SPPlaceholderCell *cell = [tableView dequeueReusableCellWithIdentifier:placeCellID];
        return cell;
    }
    
}

// 下拉刷新方法
- (void)pullDownData {
    
    _page = 1;
    ESWeak(self, weakSelf);
    NSDictionary *paramas = @{@"uid":userInfo.uid,@"p":[NSString stringWithFormat:@"%d",_page]};
    self.isHave = YES;
    [YJProgressHUD showProgress:@"加载中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Personage/myticket" sucessBlock:^(id content) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"-----优惠券数据----%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            [weakSelf dealPullDownDataWithDict:dict];
        }else {
            self.isHave = NO;
            [self.mainTableView reloadData];
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
        
    } failure:^(NSError *error) {
         self.isHave = NO;
        [self.mainTableView reloadData];
        [YJProgressHUD showMessage:@"加载失败" inView:self.view];
    }];
}

// 处理下拉刷新数据
- (void)dealPullDownDataWithDict:(NSDictionary *)dict {
    [_mainTableView.mj_footer resetNoMoreData];
    [_ticketArray removeAllObjects];
    NSArray *tempArr = dict[@"list"];
    if (tempArr.count == 0) {
        self.isHave = NO;
    }
    for (NSDictionary *dict in tempArr) {
        LZTicketModel *ticketModel = [[LZTicketModel alloc] initWithDict:[dict mutableCopy]];
        [_ticketArray addObject:ticketModel];
    }
    [_mainTableView reloadData];
}


// 上啦加载
- (void)pullUpData {
    _page ++;
    ESWeak(self, weakSelf);
    NSDictionary *paramas = @{@"uid":userInfo.uid,@"p":[NSString stringWithFormat:@"%d",_page]};
    [YJProgressHUD showProgress:@"加载中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Personage/myticket" sucessBlock:^(id content) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"-----优惠券数据----%@",dict);
        if ([dict[@"code"] intValue] == 1) {
            [weakSelf dealPullUpDataWithDict:dict];
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
        }else {
            
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
        
    } failure:^(NSError *error) {
        
        [YJProgressHUD showMessage:@"加载失败" inView:self.view];
    }];
}

// 处理上啦加载数据
- (void)dealPullUpDataWithDict:(NSDictionary *)dict {
    [_mainTableView.mj_footer resetNoMoreData];
    NSArray *tempArr = dict[@"list"];
    if (tempArr.count == 0) {
        [YJProgressHUD showMessage:@"没有更多数据了~" inView:self.view];
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (tempArr.count <= 10) {
        [_mainTableView.mj_footer endRefreshingWithNoMoreData];
    }
    for (NSDictionary *dict in tempArr) {
        LZTicketModel *ticketModel = [[LZTicketModel alloc] initWithDict:[dict mutableCopy]];
        [_ticketArray addObject:ticketModel];
    }
    [self.mainTableView reloadData];
}

@end
